<?php
namespace Admin\Service;

use Think\Service\BasicService;
use Admin\Util\lib\YouZanApiFunction;

class ERPService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($where, $model = array()) {
        return $this->db->where($where)->save($model);
    }

    public function find($condition) {
        return $this->db->where($condition)->find();
    }

    public function query($condition, $NUM = 20) {
        return $this->db->where($condition)->limit($NUM)->select();
    }

    public function AddAll($model, $model1, $where) {
        //无数据更新
        if (!$model) {
            $this->db->commit();
            return 2;
        }
        $this->db->startTrans();
        $Hdata = $this->db->where($where)->find();
        $DELOK = 1;
        $DELINFO = 1;
        if ($Hdata) {
            $DELOK = $this->db->where($where)->delete();
            $this->db = M('yz_order_info');
            $where1['t_yz_order.status'] = $where['status'];
            $DELdata = $this->db->field('t_yz_order_info.id')
                ->join('t_yz_order on t_yz_order_info.oid = t_yz_order.tid')
                ->where($where1)->select();
            $where2 = $where = 'id in(' . implode(',', $DELdata) . ')';
            dump($DELdata);

            dump($where2);
            dump($this->db->_sql());
            die();
            $DELINFO = $this->db->where($where2)->delete();

            $this->db = M('yz_order');
        }

        if ($DELOK && $DELINFO) {
            foreach ($model as $k => $v) {
                $ADDOK = $this->db->addAll($v);
                if (!$ADDOK) {
                    $this->db->rollback();
                    return 0;
                }
            }
            foreach ($model1 as $k1 => $v1) {
                $ADDOK1 = $this->db->addAll($v1);
                if (!$ADDOK1) {
                    $this->db->rollback();
                    return 0;
                }
            }
            $this->db->commit();
            return 1;
        } else {
            $this->db->rollback();
            return 0;
        }

    }

    /*
     * 拼接订单数据显示
     */
    public function GetOrderData($OrderData = array(), $access_token = NULL) {
        foreach ($OrderData as $k => $v) {
            $OK = $this->db->where(array('wyy_djbh' => $v['tid']))->find();
            if ($OK) {
                unset($OrderData[$k]);
                continue;
            }
            $ISOK = 1;
            $datas = array();
            foreach ($v['goodsdata'] as $k1 => $v1) {
                $CODE = M('goods_code')->where(array('SHOP_CODE' => $v1['alias']))->find();
                if ($CODE) {
                    $v1['CODE'] = $CODE['ERP_CODE'];
                } else {
                    //有赞是否有内码
                    $API = new  YouZanApiFunction();
                    $PF_DATA = $API->Get_YZ_PF_Goods($v1['alias'], $access_token);
                    if (strlen($PF_DATA['response']['goods_no']) > 2) {
                        $v1['CODE'] = $PF_DATA['response']['goods_no'];
                        $kx_code = M('kx_all_goods_code')->where(array('ERP_CODE' => $PF_DATA['response']['goods_no']))->find();
                        if ($kx_code) {
                            $save['ERP_CODE'] = $PF_DATA['response']['goods_no'];
                            $save['NAME'] = $kx_code['NAME'];
                            $save['SHOP_CODE'] = $v1['alias'];
                            $save['CREATETIME'] = date('Y-m-d H:i:s');
                            $save['SKU1'] = $kx_code['PFH'];
                            $save['SKU2'] = $kx_code['GG'];
                            $save['SKU3'] = $kx_code['CJ'];
                            $save['ID'] = $v1['alias'];
                            M('goods_code')->add($save);
                        }
                    } else {
                        $ISOK = 0;
                        $v1['CODE'] = 0;
                    }
                }
                $v1 = $this->Goods_BreakUp($v1);
                $datas[] = $v1;

            }

            $yddata = M('yd_code')->where(array('SHOP_CODE' => $v['buyer_id']))->find();
            if ($yddata) {
                $OrderData[$k]['YD_ERP_CODE'] = $yddata['ERP_CODE'];
            } else {
                $OrderData[$k]['YD_ERP_CODE'] = 0;
                $ISOK = 0;
            }
            $OrderData[$k]['goodsdata'] = $datas;
            $OrderData[$k]['red'] = $ISOK ? 0 : 1;
            $YZ_ORDER_STATE = C('YZ_ORDER_STATE');
            $OrderData[$k]['status'] = $YZ_ORDER_STATE[$OrderData[$k]['status']];
        }
        return $OrderData;
    }

    /*
     * 商品拆分处理
     */
    public function Goods_BreakUp($data) {
        $temp = explode('_', $data['CODE']);
        if (count($temp)== 2) {
            $data['title'] =  $data['title']."\r\n《特殊拆分商品，原商品ERP内码".$data['CODE']."》";
            $data['CODE'] = $temp[0];
            $data['price'] = $data['price']/$temp[1];
            $data['num'] = $temp[1]*$data['num'];
        }
        return $data;

    }

    /*
     *匹配药店内码列表
     */

    public function MatchYDCODE($where, $limit) {
        $data = $this->db->join('LEFT JOIN t_yd_code on t_yd_code.SHOP_CODE = t_yz_all_yd.SHOP_CODE ')
            ->where($where)
            ->limit($limit['start'], $limit['end'])
            ->field('t_yz_all_yd.*,t_yd_code.ERP_CODE as ERP_CODE')
            ->order('ERP_CODE')
            ->select();
        return $data;
    }

    public function getcount($condition) {
        $data = $this->db->where($condition)->count();;
        return $data;
    }

    /*
     * 导入erp订单
     */
    public function LeadInOrder($DATA) {
        foreach ($DATA as $k => $v) {
            $OK = $this->db->where(array('wyy_djbh' => $v['tid']))->find();
            if ($OK) {
                unset($DATA[$k]);
                continue;
            }
            if ($v['red'] == 0) {
                $save = null;
                $save['wyy_djbh'] = $v['tid'];
                $save['wyy_dwbh'] = $v['buyer_id'];
                $save['danwbh'] = $v['YD_ERP_CODE'];
                $save['wyy_Date'] = $v['pay_time'];
                $save['wyy_Agio'] = 100;
                $save['wyy_je'] = $v['payment'];
                $save['wyy_AgioTotal'] = $v['payment'];
                $save['shouhr'] = $v['receiver_name'];
                $save['shouhdz'] = $v['receiver_state'] . $v['receiver_city'] . $v['receiver_district'] . $v['receiver_address'];
                $save['tel'] = $v['receiver_mobile'];
                $save['wyy_In'] = 0;
                $save['status'] = 0;
                $save['wyy_Note'] = $v['buyer_message'];
                $save['FreePostStatus'] = 0;
                $save['FreePostAmount'] = 0;
                $save['xgdjbh'] = NULL;
                $ALLSAVE[] = $save;
                foreach ($v['goodsdata'] as $k1 => $v1) {
                    $save1 = null;
                    $save1['wyy_djbh'] = $v['tid'];
                    $save1['wyy_Sort'] = $k1 + 1;
                    $save1['wyyspid'] = $v1['alias'];
                    $save1['spbh'] = $v1['CODE'];
                    $save1['price'] = $v1['price'];
                    $save1['dw'] = $v1['unit'];
                    $save1['shl'] = $v1['num'];
                    $save1['wyy_In'] = 0;
                    $ALLSAVE1[] = $save1;
                }
                unset($DATA[$k]);
            }
        }
        $this->db->startTrans();
        $OK = $this->db->addAll($ALLSAVE);

        if ($OK) {
            $this->db = M('kx_erp_order_info');
            $OK1 = $this->db->addAll($ALLSAVE1);
            if ($OK1) {
                $this->db->commit();
                return array('code' => 1, 'DATA' => $DATA);
            }
        }
        $this->db->rollback();
        return array('code' => 0, 'DATA' => $DATA);
    }


}

?>